/**
 * Created by Administrator on 2019/2/26.
 */
function almoney($scope, $route, CertusFormAjax, CertusJsonAjax, $location, $http, $cookies, $routeParams, $q,backlogPersonList ){


    $backlogPersonList = backlogPersonList;
    $$scope = $scope;
    //初始化 相关 数据
    $scope.dataList = {}
    $scope.expenditureEntity = {};

    var nowProjName;

    function isSaveProjName(element) {
        return element.projName.indexOf(nowProjName) > -1;
    }

    $scope.companyCodeChange = function (a) {
        for (var cs in $scope.dataList.companySelect) {

            if ($scope.dataList.companySelect[cs].code == a) {
                $scope.expenditureEntity.typeName = $scope.dataList.companySelect[cs].name;
            }
        }
    }

    var nowCost;

    function isSaveProjCost(element) {
        return element.budgetNo.indexOf(nowCost) > -1;
    }
    //监听项目名称
    $scope.$watch("expenditureEntity.costCode", function (newValue, old, scope) {
        if(newValue){
            CertusFormAjax.ajax("relatedCostCodeProj", {param: newValue}, function (res) {

                if(res){
                    scope.expenditureEntity.projName = res.data.projName;
                    scope.expenditureEntity.projId = res.data.projId;
                }else{
                    scope.expenditureEntity.projName = "";
                    scope.expenditureEntity.projId = "";
                }
            })
        }
    })



    // //监听项目名称
    // $scope.$watch("expenditureEntity.projName", function (newValue, old, scope) {
    //     if (old && newValue) {
    //         if (newValue.indexOf(old) > -1) {
    //             nowProjName = newValue;
    //             scope.dataList.projNameSelect = scope.dataList.projNameSelect.filter(isSaveProjName);
    //             if (scope.dataList.projNameSelect.length) {
    //                 if (scope.dataList.projNameSelect.length == 1) {
    //                     scope.expenditureEntity.projId = scope.dataList.projNameSelect[0].projId;
    //                     scope.expenditureEntity.projName = scope.dataList.projNameSelect[0].projName;
    //                 }
    //             }
    //             if(!scope.dataList.projNameSelect || scope.dataList.projNameSelect.length == 0){
    //                 scope.expenditureEntity.projId = "";
    //                 scope.expenditureEntity.projName = "";
    //             }
    //             return;
    //         }
    //     }
    //
    //     //直接去数据库 查询
    //     CertusFormAjax.ajax("relatedProj", {param: newValue}, function (res) {
    //         scope.dataList.projNameSelect = res.data;
    //         if (scope.dataList.projNameSelect.length) {
    //             if (scope.dataList.projNameSelect.length == 1) {
    //                 scope.expenditureEntity.projId = scope.dataList.projNameSelect[0].projId;
    //                 scope.expenditureEntity.projName = scope.dataList.projNameSelect[0].projName;
    //             }
    //         }
    //     })
    // })

    //付款方式
    $scope.dataList.costTypeSelect = [
        {name: "支票", code: "check"},
        {name: "电汇", code: "dh"},
        {name: "转账", code: "zz"},
        {name: "其他", code: "qt"}

    ]

    $scope.costTypeChange = function(a){
        for(var j in $scope.dataList.costTypeSelect){
            if($scope.dataList.costTypeSelect[j].code == a){
                $scope.expenditureEntity.paymentMethodName = $scope.dataList.costTypeSelect[j].name;
            }
        }
    }

    //费用分类
    $scope.dataList.costSelect = [
        {name: "火车/汽车", code: "a"},
        {name: "飞机", code: "b"},
        {name: "交通", code: "c"},
        {name: "住宿", code: "d"},
        {name: "出差补助", code: "e"},
        {name: "市内交通费", code: "f"},
        {name: "办公费", code: "h"},
        {name: "低值易耗品", code: "i"},
        {name: "餐费", code: "j"},
        {name: "通讯费", code: "k"},
        {name: "车辆费", code: "l"},
        {name: "水电费", code: "m"},
        {name: "物管费", code: "o"},
        {name: "房屋租赁费", code: "p"},
        {name: "其他租赁费", code: "q"},
        {name: "办公电脑补贴", code: "r"},
        {name: "对外服务费", code: "s"},
        {name: "对外咨询费", code: "t"},
        {name: "外聘费用", code: "u"},
        {name: "证书补贴", code: "v"},
        {name: "外部培训费", code: "w"},
        {name: "礼品", code: "x"},
        {name: "招待费", code: "y"},
]

    $scope.costChange = function(a){
        for(var i in $scope.dataList.costSelect){
            if($scope.dataList.costSelect[i].code == a){
                $scope.expenditureEntity.expenseClassFicationName = $scope.dataList.costSelect[i].name;
            }
        }
    }

    var cusProjName;

    function isSaveCusName(element) {
        return element.comName.indexOf(cusProjName) > -1;
    }

    var nowTriburse;
    function isSaveRetribursementName(element) {
        return element.comName.indexOf(nowTriburse) > -1;
    }
    //费用申请编号 costCode
    // $scope.$watch("expenditureEntity.costCode", function (newValue, old, scope) {
    //     if (old && newValue) {
    //         if (newValue.indexOf(old) > -1) {
    //             nowTriburse = newValue;
    //             scope.dataList.triburseSelect = scope.dataList.triburseSelect.filter(isSaveRetribursementName);
    //             if (scope.dataList.triburseSelect.length) {
    //                 if (scope.dataList.triburseSelect.length == 1) {
    //                     // scope.expenditureEntity.borrowCode = scope.dataList.cusCompSelect[0].id;
    //                     scope.expenditureEntity.borrowCode = scope.dataList.triburseSelect[0].comName;
    //                 }
    //             }
    //             return;
    //         }
    //     }
    //     //直接去数据库 查询
    //     CertusFormAjax.ajax("expenditure/borrowingEntityList", {borrowCode: newValue}, function (res) {
    //         scope.dataList.triburseSelect = res.data;
    //         if (scope.dataList.triburseSelect.length) {
    //             if (scope.dataList.triburseSelect.length == 1) {
    //                 // scope.expenditureEntity.customUnitId = scope.dataList.cusCompSelect[0].id;
    //                 scope.expenditureEntity.borrowCode = scope.dataList.triburseSelect[0].comName;
    //             }
    //         }
    //     })
    // })

    //提交流程
    $scope.zdbxlcSubmit = function () {
        if (certusValidateNotnull()) {
            if (!$scope.expenditureEntity.costCode) {
                layer.msg("费用申请编号,填写不正确，请检查.");
                return;
            }

            if (!$scope.expenditureEntity.projId) {
                layer.msg("项目未填写");
                return;
            }

            $scope.expenditureStepEntity = {};
            CertusJsonAjax.ajax("expenditure/zdbxlcSubmit", $scope.expenditureEntity, function (res) {
                if (res.data == "success") {

                    findBacklogPersonHead(CertusFormAjax, "XJDKLC");
                    window.history.back();
                    return;
                }
                layer.msg("提交遇到错误")
            })
        }
    }
    //校验 俩个 id


}


function listMoney($scope, $route, CertusFormAjax, CertusJsonAjax, $location, $http, $cookies, $routeParams, $q){
    var dataInit = CertusJsonAjax.dataInit("expenditure/listExpenditure",null,slmoneyColumns,'slmoneyContainer','slmoneyToolBarContainer');

    var picker3 = $('#startTime').datetimepicker({
        format: 'YYYY-MM-DD',
        locale: moment.locale('zh-cn')
    })

    var picker3 = $('#endTime').datetimepicker({
        format: 'YYYY-MM-DD',
        locale: moment.locale('zh-cn')
    })

    $scope.searchData = function(){

        dataInit.parameters = {
            "userName":$("#userNameId").val(),
            "projName":$("#projName").val(),
            "startTime":$("#startTime").val(),
            "endTime":$("#endTime").val()
        }
        dataInit.refresh(true);
    }
}

function tlmoneys($scope, $route, CertusFormAjax, CertusJsonAjax, $location, $http, $cookies, $routeParams, $q){
    CertusFormAjax.ajax("expenditure/queryReceptionByCode",{rtCode:$routeParams.rtCode},function(res){

        $scope.expenditureEntity = res.data;

        $scope.processs = res.data.processs;

        $scope.rmb = upDigit($scope.expenditureEntity.paymentAmount);

        $scope.getsssbbb = function(bb){
            if(bb == "starting"){
                return "正在进行中";
            }else if(bb == "denied"){
                return "否决";
            }else if(bb == "sentback"){
                return "被退";
            }else if(bb == "others"){
                return "关闭";
            }else if(bb == "completed"){
                return "完成";    }else if(bb == "terminated"){        return "终结";    }    else if(bb == "cancelled"){        return "通过";}
            return "待办";
        }
    })
}

// RMB大写转换
function upDigit(n) {
    var fraction = ['角', '分'];
    var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
    var unit = [
        ['元', '万', '亿'],
        ['', '拾', '佰', '仟']
    ];
    var head = n < 0 ? '欠' : '';
    n = Math.abs(n);

    var s = '';

    for (var i = 0; i < fraction.length; i++) {
        s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
    }
    s = s || '整';
    n = Math.floor(n);

    for (var i = 0; i < unit[0].length && n > 0; i++) {
        var p = '';
        for (var j = 0; j < unit[1].length && n > 0; j++) {
            p = digit[n % 10] + unit[1][j] + p;
            n = Math.floor(n / 10);
        }
        s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
    }
    return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整');
}

var slmoneyColumns = [{
    id: 'projName',
    title: '项目名称',
    type: 'string',
    width: 150,
    columnClass: 'text-center',
}, {
    id: 'paymentMethodName',
    title: '付款方式',
    type: 'string',
    width: 75,
    columnClass: 'text-center',
}, {
    id: 'expenseClassFicationName',
    title: '费用分类',
    type: 'string',
    width: 100,
    columnClass: 'text-center',
}, {
    id: 'paymentAmount',
    title: '付款金额',
    type: 'string',
    width: 150,
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '￥' + value + '元';
        return content;
      }
}, {
    id: 'applyManName',
    title: '申请人',
    type: 'string',
    width: 75,
    columnClass: 'text-center',
}, {
    id: 'status',
    title: '状态',
    type: 'string',
    width: 75,
    columnClass: 'text-center',
    resolution:function(value, record, column, grid, dataNo, columnNo){
        var content="";
        if (record.status == "end"){
            return "完成";
        }
        return "未完成";
    }
}, {
    id: 'createTime',
    title: '申请时间',
    type: 'string',
    width: 110,
    columnClass: 'text-center',
    resolution:function(value, record, column, grid, dataNo, columnNo){
        var content="";
        if (record.createTime!=null){
            var date = new Date(record.createTime)
            content = $.fn.dlshouwen.grid.tools.dateFormat(date, "yyyy-MM-dd");
        }
        return content;
    }
}, {
    id: 'payContent',
    title: '支付内容/备注说明',
    type: 'string',
    columnClass: 'text-left',
}, {
    id: 'operation',
    title: '操作',
    width: 70,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '';
        content += '<a class="btn btn-xs btn-primary" href="#/slmoney/'+record.expenditureCode+'"><i class="icon-eye-open"></i>  查看</a>';
        // content += '  ';
        // content += '<button class="btn btn-xs btn-danger" onclick=""><i class="fa fa-trash-o"></i>  删除</button>';
        return content;
    }
}];